<template>
  <div class="app-container">
    <!-- 表格 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="数据加载中"
      border
      fit
      highlight-current-row>

      <el-table-column
        label="序号"
        width="70"
        align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="sno" label="学号" width="80"/>
      <el-table-column prop="name" label="姓名" width="120"/>
      <el-table-column label="性别" width="80" maxlength="80">
        <template slot-scope="scope">
          {{ scope.row.gender===1?'男':'女' }}
        </template>
      </el-table-column>
      <el-table-column prop="nationality" label="民族" />
      <el-table-column label="政治面貌" width="80">
        <template slot-scope="scope">
          <div v-if="scope.row.politicStatus==='TY'">
            团员
          </div>
          <div v-else-if="scope.row.politicStatus==='DY'">
            党员
          </div>
          <div v-else-if="scope.row.politicStatus==='QZ'">
            群众
          </div>
          <div v-else>其他</div>
        </template>
      </el-table-column>
      <el-table-column prop="birthday" label="生日" width="120"/>
      <el-table-column prop="idno" label="身份证号" width="200" />
      <el-table-column prop="address" label="家庭地址" width="200"/>
      <el-table-column prop="gradeName" label="年级" width="120"/>
      <el-table-column prop="majorName" label="专业" width="180"/>
      <el-table-column prop="className" label="班级" width="120"/>
      <el-table-column prop="phone" label="联系电话" width="120"/>
      <el-table-column prop="enrollDate" label="入学日期" width="120"/>
      <el-table-column prop="graduateDate" label="毕业日期" width="120"/>
      <el-table-column prop="remark" label="备注" width="200"/>
      <el-table-column prop="createTime" label="添加时间" width="180"/>

      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <router-link :to="'/student/edit/'+scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit">修改</el-button>
          </router-link>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeDataById(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="fetchData"
    />
  </div>
</template>

<script>
import student from '@/api/student/student'
export default {

  data() { // 定义数据
    return {
      listLoading: true, // 是否显示loading信息
      list: null, // 数据列表
      total: 0, // 总记录数
      page: 1, // 页码
      limit: 5, // 每页记录数
      searchObj: {}// 查询条件
    }
  },

  created() { // 当页面加载时获取数据
    this.fetchData()
  },

  methods: {
    fetchData(page = 1) { // 调用api层获取数据库中的数据
      // 当点击分页组件的切换按钮的时候，会传输一个当前页码的参数page
      // 解决分页无效问题
      this.page = page
      this.listLoading = true
      student.getPageList(this.page, this.limit, this.searchObj).then(response => {
        // debugger 设置断点调试
        if (response.success === true) {
          this.list = response.data.rows
          this.total = response.data.total
        }
        this.listLoading = false
      })
    },
    removeDataById(id) {
    // debugger
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return student.removeById(id)
      }).then(() => { // 如果上一个then成功则执行此处的then回调
        this.fetchData()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch((response) => { // 失败
        if (response === 'cancel') {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        }
      })
    }
  }
}
</script>
